{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7e042cc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def matrix_multiple(x, y):\n",
    "    result = [[0 for i in range(len(y[0]))] for i in range(len(x))]\n",
    "    #初始化result二维列表，分别以x的行数，y的列数为行数，列数\n",
    "    print (result)#测试初始化结果，实际应用请注释掉这行\n",
    "    if type(x[0])==int:\n",
    "        if type(y[0])==int:\n",
    "            result=[x[0]*y[0]]\n",
    "        else:\n",
    "            for i in range(len(x)):\n",
    "                for j in range(len(y[0])):\n",
    "                    for k in range(len(y)):\n",
    "                        print(result[i][j])\n",
    "                        result[i][j] +=x[i] * y[k][j]\n",
    "    elif type(y[0])==int:\n",
    "        for i in range(len(x)):\n",
    "                for j in range(len(y[0])):\n",
    "                    for k in range(len(y)):\n",
    "                        result[i][j] += x[i][k] * y[k]\n",
    "    else:\n",
    "        for i in range(len(x)):\n",
    "            for j in range(len(y[0])):\n",
    "                for k in range(len(y)):\n",
    "                    result[i][j] += x[i][k] * y[k][j]\n",
    "    print (result)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "ffd50a4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0]]\n",
      "[[10]]\n"
     ]
    }
   ],
   "source": [
    "assert matrix_multiple([[2]], [[5]]) == [[10]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "be9f5ddb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0]]\n",
      "[[11]]\n"
     ]
    }
   ],
   "source": [
    "assert matrix_multiple([[1, 3]], [[5], [2]]) == [[11]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "0ca0293d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=[[1, 3]]\n",
    "len(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cb6af218",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0, 0, 0], [0, 0, 0]]\n",
      "[[23, 21, 25], [18, 16, 18]]\n"
     ]
    }
   ],
   "source": [
    "assert matrix_multiple([[3, 5], [2, 4]], [[1, 2, 5], [4, 3, 2]]) == [[23, 21, 25], [18, 16, 18]]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
